package com.ext.aic.utils;

import android.content.Context;

import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.ext.aic.R;
import com.ext.aic.adapter.MainViewPagerAdapter;
import com.ext.aic.components.SpecialTab;
import com.ext.aic.components.SpecialTabRound;
import com.ext.aic.constants.EBusConstants;
import com.ext.aic.databinding.MainBinding;
import com.ext.aic.ui.CreateActivity;
import com.eyinfo.android_pure_utils.ebus.EBus;

import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;

public class MainPageNavigationUtils {
    private static volatile MainPageNavigationUtils instance;

    public static MainPageNavigationUtils getInstance() {
        if (instance == null) {
            synchronized (MainPageNavigationUtils.class) {
                if (instance == null) {
                    instance = new MainPageNavigationUtils();
                }
            }
        }
        return instance;
    }

    private NavigationController navigationController;

    private String getContentCenterText(Context context) {
        return context.getString(R.string.content_center);
    }

    private String getAiSeparateText(Context context) {
        return context.getString(R.string.ai_separate);
    }

    private String getCreationText(Context context) {
        return context.getString(R.string.copywriting);
    }

    private String getGettingCustomersText(Context context) {
        return context.getString(R.string.getting_customers);
    }

    private String getMineText(Context context) {
        return context.getString(R.string.mine);
    }

    /**
     * 设置消息数量
     *
     * @param tabIndex tab索引（包括中间按钮）
     * @param number   消息数量
     */
    public void bindMessageNumber(int tabIndex, int number) {
        if (navigationController == null) {
            return;
        }
        navigationController.setMessageNumber(tabIndex, number);
        navigationController.setHasMessage(tabIndex, true);
    }

    public void init(FragmentActivity fragmentActivity, MainBinding binding) {
        navigationController = binding.bottomTab.custom()
                .addItem(newItem(fragmentActivity, R.drawable.content_normal, R.drawable.content_press, getContentCenterText(fragmentActivity)))
                .addItem(newItem(fragmentActivity, R.drawable.ai_separate_normal, R.drawable.ai_separate_press, getAiSeparateText(fragmentActivity)))
                .addItem(newRoundItem(fragmentActivity, R.drawable.create_normal, R.drawable.create_press, getCreationText(fragmentActivity)))
                .addItem(newItem(fragmentActivity, R.drawable.getting_customers_normal, R.drawable.getting_customers_press, getGettingCustomersText(fragmentActivity)))
                .addItem(newItem(fragmentActivity, R.drawable.mine_normal, R.drawable.mine_press, getMineText(fragmentActivity)))
                .build();
        setupViewPager(binding, fragmentActivity);
    }

    private void setupViewPager(MainBinding binding, FragmentActivity fragmentActivity) {
        binding.viewPager.setOffscreenPageLimit(3);
        binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                if (position < 2) {
                    navigationController.setSelect(position, false);
                } else if (position == 2) {
                    navigationController.setSelect(3, false);
                } else if (position == 3) {
                    navigationController.setSelect(4, false);
                }
            }
        });

        navigationController.addTabItemSelectedListener(new OnTabItemSelectedListener() {
            @Override
            public void onSelected(int index, int old) {
                if (index == 1) {
                    EBus.getInstance().post(EBusConstants.startAccessibilitySettings);
                }
                if (index == 2) {
                    onCenterClick(fragmentActivity);
                    if (old < 2) {
                        binding.viewPager.setCurrentItem(old, true);
                    } else if (old > 2) {
                        binding.viewPager.setCurrentItem(old - 1, true);
                    }
                    navigationController.setSelect(old);
                } else {
                    binding.viewPager.setCurrentItem(index < 2 ? index : (index - 1), true);
                }
            }

            @Override
            public void onRepeat(int index) {
                if (index == 2) {
                    onCenterClick(fragmentActivity);
                }
            }
        });

        MainViewPagerAdapter adapter = new MainViewPagerAdapter(fragmentActivity);
        binding.viewPager.setAdapter(adapter);
        binding.viewPager.setUserInputEnabled(false);
    }

    private void onCenterClick(Context context) {
        RedirectUtils.getInstance().startActivity(context, CreateActivity.class);
    }

    private BaseTabItem newRoundItem(Context context, int drawable, int checkedDrawable, String text) {
        SpecialTabRound mainTab = new SpecialTabRound(context);
        mainTab.initialize(drawable, checkedDrawable, text);
        mainTab.setTextDefaultColor(0xFF888888);
        mainTab.setTextCheckedColor(0xFF1296db);
        return mainTab;
    }

    private BaseTabItem newItem(Context context, int drawable, int checkedDrawable, String text) {
        SpecialTab mainTab = new SpecialTab(context);
        mainTab.initialize(drawable, checkedDrawable, text);
        mainTab.setTextDefaultColor(0xFF888888);
        mainTab.setTextCheckedColor(0xFF1296db);
        return mainTab;
    }

    public void switchTab(int index) {
        if (navigationController == null) {
            return;
        }
        int itemCount = navigationController.getItemCount();
        if (index < 0 || index >= itemCount) {
            return;
        }
        navigationController.setSelect(index);
    }
}
